package com.billard.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    private static final Logger log = LoggerFactory.getLogger(WebSocketConfig.class);

    /**
     * 配置消息代理
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 客户端订阅前缀
        registry.enableSimpleBroker("/topic", "/queue","/");
        // 客户端发送前缀
        registry.setApplicationDestinationPrefixes("/app");
        // 用户前缀（私聊）
        registry.setUserDestinationPrefix("/user");
    }
    /**
     * 注册stomp的端点，发布或者订阅消息时需要连接此端点
     */

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        log.info("注册stomp的端点");
        // 注册stomp的端点，发布或者订阅消息时需要连接此端点
        registry.addEndpoint("/ws").setAllowedOriginPatterns("*").withSockJS();
    }
}
